/**
 * Created by limeng on 16/6/2.
 */
delegateApp.controller("SystemConfigCtrl", function ($scope, $http, $rootScope, $filter) {
    $scope.config = {
        title : "",
        startTime : 0,
        endTime : 0,
        motionClosingTime : 0
    };
    $scope.newConfig = {
        title : "输入!",
        startTime : new Date(),
        endTime : ""
    };

    $http.get('/systemConfig/').success(function (data) {
        $scope.config = data;
    }).error(function () {
        bootbox.alert("获取信息失败!");
    });

    $scope.closeMotion = function () {
        bootbox.confirm("真的要结束附议吗?", function (res) {
            if (res == true) {
                $http.post("/systemConfig/closeMotion", {}).success(function (data) {
                    if (data.state == true) {
                        bootbox.alert('已结束附议', function() {
                            location.reload();
                        });
                        //$scope.config.motionClosingTime = new Date();
                    } else {
                        bootbox.alert('结束附议失败!原因是'+data.result);
                    }
                }).error(function () {
                    bootbox.alert('服务器没有响应!');
                });
            }
        });
    };

    $scope.close = function () {
        bootbox.confirm("真的要强制结束本次大会吗?", function (res) {
            if (res == true) {
                $http.post("/systemConfig/close", {}).success(function (data) {
                    if (data.state == true) {
                        bootbox.alert('大会已强制结束', function () {
                            location.reload();
                        });
                    } else {
                        bootbox.alert('结束大会失败!原因是'+data.result);
                    }
                }).error(function () {
                    bootbox.alert('服务器没有响应!');
                });
            }
        });
    };

    $scope.createNewConfig = function () {
        var postData = {};
        postData.title = $scope.newConfig.title;
        postData.endTime = $filter('date')($scope.newConfig.endTime, 'yyyy-MM-dd');
        $http.post("/systemConfig/new", postData).success(function (data) {
            if (data.state == true) {
                bootbox.alert("创建成功!", function () {
                    location.reload();
                });
            } else {
                bootbox.alert("创建失败, 原因是"+data.result);
            }
        }).error(function () {
            bootbox.alert("服务器没有响应请求!");
        });
    };

    $scope.createNewConfigDialog = function () {
        $('#createNewConfigDialog').modal();
    };

    $scope.checkNewConfig = function () {
        var now = new Date();
        var endTime = new Date($scope.config.endTime);
        endTime.setHours(0);
        endTime.setDate(endTime.getDate());
        if (now > endTime) {
            return true;
        }
        return false;
    }
});